package com.example.demo.controller;

import com.example.demo.Service.LikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/likes")
public class LikeController {

    @Autowired
    private LikeService likeService;

    @PostMapping("/{itemId}")
    public ResponseEntity<Void> addLike(@PathVariable Long itemId, @RequestParam Long userId) {
        likeService.addLike(itemId, userId);
        System.out.println("添加成功");
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/{itemId}")
    public ResponseEntity<Void> removeLike(@PathVariable Long itemId, @RequestParam Long userId) {
        likeService.removeLike(itemId, userId);
        System.out.println("删除成功");
        return ResponseEntity.ok().build();
    }
    @GetMapping("/{itemId}/count")
    public ResponseEntity<Integer> getLikeCount(@PathVariable Long itemId) {
        int count = likeService.getLikeCount(itemId);
        return ResponseEntity.ok(count);
    }
    @GetMapping("/check")
    public ResponseEntity<Boolean> checkIfUserHasLiked(
            @RequestParam Long itemId,
            @RequestParam Long userId) {
        boolean exists = likeService.existsByItemIdAndUserId(itemId, userId);
        return ResponseEntity.ok(exists);
    }
    @PostMapping("/batch")
    public Map<Long, Integer> getBatchLikeCounts(@RequestBody List<Long> itemIds) {
        return likeService.getBatchLikeCounts(itemIds);
    }
}